package cn.lyjuan.android.demofive.system.fragment

import android.app.FragmentManager
import android.os.Bundle
import android.os.PersistableBundle
import android.support.v4.app.Fragment
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.widget.FrameLayout
import cn.lyjuan.android.demofive.R

/**
FragmentActivity: onCreate
DemoFragment: onAttach-activity
DemoFragment: onAttach
DemoFragment: onCreate
DemoFragment: onCreateView
DemoFragment: onViewCreated
DemoFragment: onActivityCreated
FragmentActivity: onStart
DemoFragment: onStart
FragmentActivity: onResume
DemoFragment: onResume
FragmentActivity: onResumeFragments
DemoFragment: onPause
FragmentActivity: onPause
DemoFragment: onStop
FragmentActivity: onStop
DemoFragment: onDestroyView
DemoFragment: onDestroy
DemoFragment: onDetach
FragmentActivity: onDestroy
 */
class FragmentActivity : AppCompatActivity() {

    companion object {
        val TAG = FragmentActivity::class.java.simpleName
    }

    var framelayout: FrameLayout? = null

    var fm: FragmentManager? = null

    var frag: DemoFragment? = null

    val tag = "fragment_tag"

    override fun onCreate(savedInstanceState: Bundle?) {

        Log.d(TAG, "onCreate")
        super.onCreate(savedInstanceState)

        setContentView(R.layout.system_fragment_activity)

        loadView()

        fm = this.fragmentManager

        newFrag()

        addFrag()
    }

    private fun newFrag() {
        frag = DemoFragment()
        var ble = Bundle()
        ble.putString("abc", "123")

        frag!!.arguments = ble
    }

    private fun addFrag() {
        fm!!.beginTransaction()
                .replace(R.id.system_fragment, frag, tag)
                .commit()
    }

    private fun loadView() {
        framelayout = findViewById(R.id.system_fragment)
    }


    /******* 生命周期 ********/
    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)
        Log.d(TAG, "onCreate-persistent")
    }

    override fun onSaveInstanceState(outState: Bundle?) {
        super.onSaveInstanceState(outState)
        Log.d(TAG, "onSaveInstanceState")
    }

    override fun onSaveInstanceState(outState: Bundle?, outPersistentState: PersistableBundle?) {
        super.onSaveInstanceState(outState, outPersistentState)
        Log.d(TAG, "onSaveInstanceState-persistent")
    }

    override fun onStateNotSaved() {
        super.onStateNotSaved()
        Log.d(TAG, "onStateNotSaved")
    }

    override fun onStart() {
        super.onStart()
        Log.d(TAG, "onStart")
    }

    override fun onResumeFragments() {
        super.onResumeFragments()
        Log.d(TAG, "onResumeFragments")
    }

    override fun onPause() {
        super.onPause()
        Log.d(TAG, "onPause")
    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG, "onResume")
    }

    override fun onStop() {
        super.onStop()
        Log.d(TAG, "onStop")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(TAG, "onDestroy")
    }

    override fun onAttachFragment(fragment: Fragment?) {
        super.onAttachFragment(fragment)
        Log.d(TAG, "onAttachFragment")
    }
}